﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Activation;
using System.ServiceModel;
using yq.Dictionary;
using Db;

namespace yq.Service
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class User : yq.IService.IUser
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        public yq.Dictionary.ReturnValue<string> Login(string account, string passWord)
        {
            var cnhp = new Db.DbHelper();
            ReturnValue<string> rv = new ReturnValue<string>();
            yq.Business.UserLoginDal dal = new Business.UserLoginDal();

            var info = dal.GetModel((obj) =>

                 (obj.Account == account && obj.PassWord == passWord.ExtMD5()

                 ), cnhp);

            if (info.IsLoad)
            {
                rv.True(info.UserId);
            }
            else
            {
                rv.False("账号登录失败");
            }

            return rv;
        }


        /// <summary>
        /// 注册系统管理员
        /// </summary>
        /// <param name="login"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        public ReturnValue<string> RegisterAdmin(Model.Logic.UserLogin login, Model.Logic.UserAccount account)
        {
            account.UserId = yq.Dictionary.SysPromise.AdminId;
            account.State = Enums.UserAccount.State.Normal;
            account.PurviewEnum = Enums.UserAccount.PurviewEnum.Admin;

            Model.Logic.UserInfo info = new Model.Logic.UserInfo();

            return Register(login, account, info);
        }

        /// <summary>
        /// 注册普通用户
        /// </summary>
        /// <param name="login"></param>
        /// <param name="account"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        public ReturnValue<string> RegisterUser(Model.Logic.UserLogin login, Model.Logic.UserAccount account, Model.Logic.UserInfo info)
        {
            account.UserId = "Guid".ExtNewGuid();
            if (account.State == Enums.UserAccount.State.Default)
            {
                account.State = Enums.UserAccount.State.Normal;
            }
            account.PurviewEnum = Enums.UserAccount.PurviewEnum.User;

            return Register(login, account, info);
        }

        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="login"></param>
        /// <param name="account"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        private ReturnValue<string> Register(Model.Logic.UserLogin login, Model.Logic.UserAccount account, Model.Logic.UserInfo info)
        {

            login.CreateTtime = DateTime.Now;
            account.CreateTtime = DateTime.Now;
            Func<DbHelper, yq.CaCheRefresh, ReturnValue<string>> dbfun = (cn, ca) =>
            {
                ReturnValue<string> rv = new ReturnValue<string>();
                var loginDal = new Business.UserLoginDal();
                var accountDal = new Business.UserAccountDal();

                if (loginDal.GetModel(obj => obj.Account == login.Account && true, cn).IsLoad)
                {
                    rv.False("登录账号已存在");
                    cn.Rollback();
                }
                else if (accountDal.GetModel(obj => obj.NickName == account.NickName, cn).IsLoad)
                {
                    rv.False("昵称已存在");
                    cn.Rollback();
                }
                else
                {
                    accountDal.Add(account, account.UserId, cn);

                    //注册登录账号
                    login.UserId = account.UserId;
                    login.PassWord = login.PassWord.ExtMD5();
                    loginDal.Add(login, cn);

                    info.UserId = account.UserId;
                    //添加用户详细信息
                    new Business.UserInfoDal().Add(info, info.UserId, cn);

                    cn.Commit();

                    rv.True(account.UserId);
                }

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }


        /// <summary>
        /// 查询用户
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public PageList<Model.Logic.UserAccount> SelectAccountPagerList(Search.User.AccountSeek search)
        {
            var dal = new yq.Business.UserAccountDal();

            if (string.IsNullOrEmpty(search.NickName) == false)
            {
                dal.WherePlus(obj => obj.NickName.Like(search.NickName));
            }

            if (search.PurviewEnum != Enums.UserAccount.PurviewEnum.Default)
            {
                dal.WherePlus(obj => obj.PurviewEnum == search.PurviewEnum);
            }

            if (search.State != Enums.UserAccount.State.Default)
            {
                dal.WherePlus(obj => obj.State == search.State);
            }

            return dal.GetPageList(search.Pager, new DbHelper());
        }

        /// <summary>
        /// 传入用户ID 返回用户对应的登录账号列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public IList<Model.Logic.UserLogin> SelectUserLoginList(string userId)
        {
            return new yq.Business.UserLoginDal().GetList(obj => obj.UserId == userId, new DbHelper());
        }

        /// <summary>
        /// 编辑账号和用户详细信息
        /// </summary>
        /// <param name="account"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        public ReturnValue<string> EidtAccountAndInfo(Model.Logic.UserAccount account, Model.Logic.UserInfo info)
        {

            Func<DbHelper, yq.CaCheRefresh, yq.Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                yq.Dictionary.ReturnValue<string> rv = new ReturnValue<string>();
                if (string.IsNullOrEmpty(account.NickName) == false)
                {
                    account.NickName = account.NickName.Trim();
                    yq.Business.UserAccountDal dal = new Business.UserAccountDal();
                    if (dal.GetModel(obj => obj.NickName == account.NickName && obj.UserId != account.UserId, cn).IsLoad == false)
                    {

                        dal.Update(account, cn);

                        yq.Business.UserInfoDal dalinfo = new Business.UserInfoDal();
                        dalinfo.Update(info, cn);

                        cn.Commit();
                        rv.True(account.UserId);
                    }
                    else
                    {
                        rv.False("该昵称已经被使用");
                    }
                }
                else
                {
                    rv.False("请输入昵称");
                }

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }

        /// <summary>
        /// 删除登录账号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ReturnValue<string> DelLogin(string id)
        {
            Func<DbHelper, yq.CaCheRefresh, yq.Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                yq.Dictionary.ReturnValue<string> rv = new ReturnValue<string>();

                var b = new yq.Business.UserLoginDal().Delete(id, cn);

                if (b)
                {
                    cn.Commit();
                    rv.True(id);
                }
                else
                {
                    rv.False("删除失败");
                }

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ReturnValue<string> ReatLogin(string id)
        {
            Func<DbHelper, yq.CaCheRefresh, yq.Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                yq.Dictionary.ReturnValue<string> rv = new ReturnValue<string>();
                var dal = new yq.Business.UserLoginDal();
                string pwd = new Random().Next(10000, 99999).ToString();

                var model = dal.GetModel(obj => obj.Rowid == id, cn);
                model.PassWord = pwd.ExtMD5();

                if (model.IsLoad)
                {
                    dal.Update(model, cn);
                    cn.Commit();
                    rv.True(pwd);
                }
                else
                {
                    rv.False("重置失败");
                }

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }

        /// <summary>
        /// 添加账号
        /// </summary>
        /// <param name="log"></param>
        /// <returns></returns>
        public ReturnValue<string> AddLogin(Model.Logic.UserLogin log)
        {
            log.Account = log.Account.Trim().ExtFilter();
            log.CreateTtime = DateTime.Now;

            Func<DbHelper, yq.CaCheRefresh, yq.Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                yq.Dictionary.ReturnValue<string> rv = new ReturnValue<string>();
                var dal = new yq.Business.UserLoginDal();
                var model = dal.GetModel(obj => obj.Account == log.Account, cn);
  
                if (model.IsLoad==false)
                {
                    log.PassWord = log.PassWord.ExtMD5();
                    dal.Add(log, cn);
                    cn.Commit();
                    rv.True(log.UserId);
                }
                else
                {
                    rv.False("添加失败");
                }

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }
    }
}
